;extendedClipboard.ahk
;kalleguld 10-2008
;build 5
init()



init()
{
	Global
	
	buildNum = 5
	
	Menu, TRAY, noStandard
	Menu, TRAY, add, View contents, getHelp
	Menu, TRAY, add
	Menu, TRAY, add, Options, OptOpen
	Menu, TRAY, add, Exit, OptExit
	Menu, TRAY, Default, View contents
	fileName := "extendedClipboard.ini"
	bankSize := 9
	maxHelpSize := 30
	loadOptions()
	setupShortcuts()
}


showOptions()
{
	Global
	
	; Generated using SmartGUI Creator 4.0
	Gui, Add, Text, 	x22 y30 w20 h20 , 		1
	Gui, Add, Text, 	x22 y50 w20 h20 , 		2
	Gui, Add, Text, 	x22 y70 w20 h20 , 		3
	Gui, Add, Text, 	x22 y90 w20 h20 , 		4
	Gui, Add, Text, 	x22 y110 w20 h20 ,		5
	Gui, Add, Text, 	x22 y130 w20 h20 ,		6
	Gui, Add, Text, 	x22 y150 w20 h20 ,		7
	Gui, Add, Text, 	x22 y170 w20 h20 ,		8
	Gui, Add, Text, 	x22 y190 w20 h20 ,		9
	
	Gui, Add, GroupBox, x36 y7 w130 h210 , 		Copy shortcuts:
	Gui, Add, Hotkey, 	x42 y27 	w100 h20 vCpySc1, 	%CpySc1%
	Gui, Add, Hotkey, 	x42 y47 	w100 h20 vCpySc2, 	%CpySc2%
	Gui, Add, Hotkey, 	x42 y67 	w100 h20 vCpySc3, 	%CpySc3%
	Gui, Add, Hotkey, 	x42 y87 	w100 h20 vCpySc4, 	%CpySc4%
	Gui, Add, Hotkey, 	x42 y107 	w100 h20 vCpySc5, 	%CpySc5%
	Gui, Add, Hotkey, 	x42 y127 	w100 h20 vCpySc6, 	%CpySc6%
	Gui, Add, Hotkey, 	x42 y147 	w100 h20 vCpySc7, 	%CpySc7%
	Gui, Add, Hotkey, 	x42 y167 	w100 h20 vCpySc8, 	%CpySc8%
	Gui, Add, Hotkey, 	x42 y187 	w100 h20 vCpySc9, 	%CpySc9%
	
	Gui, Add, Text, 	x135 y7 w25 h20, +Win
	
	Gui, Add, Checkbox, x145 y27 	w18 h20 vCpyWin1  Checked%CpyWin1%,
	Gui, Add, Checkbox, x145 y47 	w18 h20 vCpyWin2  Checked%CpyWin2%,
	Gui, Add, Checkbox, x145 y67 	w18 h20 vCpyWin3  Checked%CpyWin3%,
	Gui, Add, Checkbox, x145 y87 	w18 h20 vCpyWin4  Checked%CpyWin4%,
	Gui, Add, Checkbox, x145 y107 	w18 h20 vCpyWin5  Checked%CpyWin5%,
	Gui, Add, Checkbox, x145 y127 	w18 h20 vCpyWin6  Checked%CpyWin6%,
	Gui, Add, Checkbox, x145 y147 	w18 h20 vCpyWin7  Checked%CpyWin7%,
	Gui, Add, Checkbox, x145 y167 	w18 h20 vCpyWin8  Checked%CpyWin8%,
	Gui, Add, Checkbox, x145 y187 	w18 h20 vCpyWin9  Checked%CpyWin9%,
	
	Gui, Add, GroupBox, x166 y7 	w130 h210 , 		Paste Shortcuts:
	Gui, Add, Hotkey, 	x171 y27 	w100 h20 vPstSc1,  %PstSc1%
	Gui, Add, Hotkey, 	x171 y47 	w100 h20 vPstSc2,  %PstSc2%
	Gui, Add, Hotkey, 	x171 y67 	w100 h20 vPstSc3,  %PstSc3%
	Gui, Add, Hotkey, 	x171 y87 	w100 h20 vPstSc4,  %PstSc4%
	Gui, Add, Hotkey, 	x171 y107 	w100 h20 vPstSc5,  %PstSc5%
	Gui, Add, Hotkey, 	x171 y127 	w100 h20 vPstSc6,  %PstSc6%
	Gui, Add, Hotkey, 	x171 y147 	w100 h20 vPstSc7,  %PstSc7%
	Gui, Add, Hotkey, 	x171 y167 	w100 h20 vPstSc8,  %PstSc8%
	Gui, Add, Hotkey, 	x171 y187 	w100 h20 vPstSc9,  %PstSc9%
	
	Gui, Add, Text, 	x264 y7 w25 h20, +Win
	
	Gui, Add, Checkbox, x274 y27 	w18 h20 vPstWin1   Checked%PstWin1%,
	Gui, Add, Checkbox, x274 y47 	w18 h20 vPstWin2   Checked%PstWin2%,
	Gui, Add, Checkbox, x274 y67 	w18 h20 vPstWin3   Checked%PstWin3%,
	Gui, Add, Checkbox, x274 y87 	w18 h20 vPstWin4   Checked%PstWin4%,
	Gui, Add, Checkbox, x274 y107 	w18 h20 vPstWin5   Checked%PstWin5%,
	Gui, Add, Checkbox, x274 y127 	w18 h20 vPstWin6   Checked%PstWin6%,
	Gui, Add, Checkbox, x274 y147 	w18 h20 vPstWin7   Checked%PstWin7%,
	Gui, Add, Checkbox, x274 y167 	w18 h20 vPstWin8   Checked%PstWin8%,
	Gui, Add, Checkbox, x274 y187 	w18 h20 vPstWin9   Checked%PstWin9%,
	
	Gui, Add, Text, 	x306 y7 	w100 h30 ,								Extended Clipboard Build %buildNum%
	
	Gui, Add, GroupBox, x306 y47 	w110 h60 ,								Copy style
	Gui, Add, Radio, 	x316 y67 	w97 h10 vOffStyle Checked%OffStyle%,	MS Office Style
	if ( OffStyle = "1" )
	{
		ClaStyle := 0
	} else
	{
		ClaStyle := 1
	}
	Gui, Add, Radio, 	x316 y87 	w90 h10 vClaStyle Checked%ClaStyle%,	Classic Style
	
	
	Gui, Add, GroupBox, x306 y107 	w110 h50 , 					View contents:
	Gui, Add, Hotkey, 	x311 y127 	w80 h20 vChkSc, 			%ChkSc%
	Gui, Add, Checkbox, x395 y127	w18 h20 vChkWin 			Checked%ChkWin%,
	
	Gui, Add, Button, 	x306 y167 	w110 h30 gsaveO ,		 	OK
	Gui, Add, Button, 	x306 y207 	w110 h30 gcancelO, 			Cancel
	
	Gui, Show,, 		Extended Clipboard Options
}

loadOptions()
{
	Global
	
	loadOptRow("CpySc")
	loadOptRow("PstSc")
	loadOptRow("CpyWin")
	loadOptRow("PstWin")
	
	iniRead, ChkSc,  %fileName%, Global, ChkSc, 0
	iniRead, ChkWin, %fileName%, Global, ChkWin, 1
	iniRead, OffStyle, %fileName%, Global, OffStyle, 1
}
loadOptRow(row)
{
	Global
	
	local tempo, def
	loop, %bankSize% {
		if ( row = "PstSc" )
		{
			if ( A_index > 9 )
			{
				def = 
			}
			else
			{
				def = %A_index%
			}
		} else if (row = "CpySc")
		{
			if ( A_index > 9 )
			{
				def = 
			}
			else
			{
				def = +%A_index%
			}
		} else
		{
			def = 1
		}
		tempo = %row%%A_index%
		
		iniRead, %tempo%, %fileName%, %row%, %A_index%, %def%
		if ( %tempo% = "ERROR" )
		{
			%tempo% := ""
		}
	}
}


setupShortcuts()
{
	Global
	
	setScRow("Cpy")
	setScRow("Pst")
	local ChkRSc
	
	if ( ChkWin = 1 )
	{
		ChkRSc := "#" . ChkSc 
	}
	else
	{
		ChkRSc := ChkSc
	}
	Hotkey, %ChkRSc%, getHelp, ON
	
	if ( OffStyle = 1 )
	{
		Hotkey, ^c, fakeCopy, ON
		Hotkey, ^x, fakeCopy, ON
	}
}
setScRow(row)
{
	Global
	
	local tempo, sc, sca
	loop, %bankSize% {
		sca := %row%Sc%A_index%
		if ( %row%Win%A_index% = 1 )
		{
			sc = #%sca%
		}
		else
		{
			sc = %sca%
		}
		Hotkey, %sc%, doThing, ON
		
		
	}
}
unloadShortcuts()
{
	Global
	
	unloadShortcutRow("Cpy")
	unloadShortcutRow("Pst")
	if ( ChkWin = 1 )
	{
		hotkey, #%ChkSc%,, OFF
	}
	else
	{
		hotkey, %ChkSc%,, OFF
	}
	if ( OffStyle = 1 )
	{
		Hotkey, ^c,, OFF
		Hotkey, ^x,, OFF
	}
}
unloadShortcutRow(row)
{
	Global
	local Sc
	loop, %bankSize% {
		if ( %row%Win%A_index% = 1 )
		{
			Sc := "#" . %row%Sc%A_index%
		}
		else
		{
			Sc := %row%Sc%A_index%
		}
		Hotkey, %Sc%,, OFF
	}
}



saveOptions()
{
	Global
	
	saveOptRow("CpySc")
	saveOptRow("PstSc")
	saveOptRow("CpyWin")
	saveOptRow("PstWin")
	
	iniWrite, %ChkSc%,		%fileName%, Global, ChkSc
	iniWrite, %ChkWin%,		%fileName%, Global, ChkWin
	iniWrite, %OffStyle%,	%fileName%, Global, OffStyle
	
}
saveOptRow(row)
{
	Global
	
	local tempo
	loop, %bankSize% {
		tempo := %row%%A_index%
		iniWrite, %tempo%, %fileName%, %row%, %A_index%
	}
}

findHotkey(testKey, row)
{
	Global
	
	local compareKey, found = 0
	loop %bankSize% {
		if ( %row%Win%A_index% = 1 )
		{
			compareKey := "#" . %row%Sc%A_index%
		}
		else
		{
			compareKey := %row%Sc%A_index%
		}
		if ( compareKey = testKey )
		{
			found = %A_index%
			break
		}
	}
	return found
}
setExtNum(num)
{
	Global
	Local oldClip := ClipboardAll
	
	Clipboard := ""
	Send ^c
	ClipWait
	Ext%num% := ClipboardAll
	Clipboard := oldClip
}
getExtNum(num)
{
	Global
	Local oldClip := ClipboardAll	;backup the clip
	Clipboard =
	Clipboard := Ext%num%		;fill clip with data from extension bank
	ClipWait, 2, 1
	Send ^v						;paste the new clipboard
	sleep, 500
	Clipboard := oldClip		;restore clip from backup	
}

startFakeCopy(Hotkey)
{
	Global
	
	local high, low
	loop, %bankSize% {
		high := bankSize - A_index
		low := high -1
		if ( low = 0 )
		{
			break
		}
		ext%high% := ext%low%
	}
	ext1 := ClipboardAll
	SendPlay %Hotkey%
	
}

getHelpTrayPopup()
{
	Global
	
	local helpText, lastEmpty, oneLine
	oldClip := ClipboardAll
	helpText := "C) " . shorten(Clipboard)
	lastEmpty := 0
	
			;we have to do some semi-ugly hacks here, to convert the binary
			;data from ClipboardAll into text. This is why we need the
			;clipboard as a placeholder
	Loop %bankSize% {
		Clipboard := Ext%A_Index%
		
		if ( lastEmpty = 0 )
		{
			helpText := helpText . "`r`n"
		}
		
		oneLine := shorten(Clipboard)
		
		if (strLen(oneLine)) 
		{
			
			helpText := helpText . A_Index . ") " . oneLine
			lastEmpty := 0
		} 
		else 
		{
			if ( lastEmpty = 0 )
			{
				helpText := helpText . "`r`n"
			}
			lastEmpty := 1
		}
		
	}
	
	;Display the helpText in a trayTip for 20 seconds with no sound
	TrayTip, Extended Clipboard contents:, %helpText%, 20, 16
	
	Clipboard := oldClip	;Restore the clipboard
}
shorten(str)
{
	Global
	local size, suffix
	size := inStr(str, "`r")
	if ( size )
	{
		suffix := "..."
		size := size - 1
	} 
	else if ( strLen(str) > maxHelpSize )
	{
		suffix := "..."
		size := maxHelpSize
	}
	else
	{
		suffix := ""
		size := strLen(str)
	}

	
	if ( strLen(str) )
	{
		return subStr(str, 1, size) . suffix
	}
	else
	{
		return
	}
}



saveO:
	Gui, Submit
	Gui, Destroy
	unloadShortcuts()
	saveOptions()
	setupShortcuts()
return
cancelO:
	Gui, Cancel
	Gui, Destroy
return

doThing:
	;Arrgh, damn these stupid hacks. Why can't ahk just let labels and gotos die already?
	;Now I have to find out what key the user pressed, because it couldn't be passed as a parameter
	
	testkey := A_ThisHotKey
	found := findHotkey(testkey, "Pst")
	if ( found > 0 )
	{
		getExtNum(found)
	}
	else
	{
		found := findHotkey(testkey, "Cpy")
		if ( found > 0 )
		{
			setExtNum(found)
		}
	}
return

getHelp:
	getHelpTrayPopup()
return

fakeCopy:
	startFakeCopy(A_ThisHotkey)
return

OptOpen:
	showOptions()
return

OptExit:
	exitApp
return
